layui.config({
    base: '../../layui/' //此处路径请自行处理, 可以使用绝对路径
}).extend({
    formSelects: 'formSelects-v4'
}).use(['jquery', 'formSelects', 'form','layer'], function () {
    const form = layui.form
        , layer = layui.layer
        , $ = layui.jquery;
    form.on('submit(save)', function (data) {
        const params = data.field;
        submit($, params);
        return false;

    });
    function submit($, params) {
        $.ajax({
            url: '/admNacos',
            data: JSON.stringify(params),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            type: "put",
            success: function (result) {
                layer.msg("修改成功", {icon: 1, time: 1000}, function () {
                    CloseWin();
                })
            }
        })
    }
    $("#close").on('click', function () {
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    //关闭页面
    function CloseWin() {
        parent.location.reload(); // 父页面刷新
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }

    $(function () {
        getDocker("")
        setTimeout(() => {
            getDb("")
        }, 500);

    })

    dockerNameInput = function (e) {
        const val = $("#dockerName").val();
        getDocker(val)
    }
    let dockerData

    function getDocker(dockerName) {
        $.ajax({
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/admDocker/getAll?name=" + dockerName,//url
            success: function (result) {
                dockerData = result
                $("#dockerId").html("");
                var html = "";
                $("#dockerId").html("<option value=\"\">请选择docker</option>");
                if (result != null) {
                    $.each(result, function (index, item) {
                        html += "<option  value='" + item.id + "'>" + item.dockerName + "</option>";
                    })
                }
                $("#dockerId").append(html);
                if (dockerName != null && dockerName != '') {
                    $(".dockerId").find(".layui-form-select").addClass("layui-form-selected")
                }
                const name = $("#dockerName").val();
                if (name != null && name != '') {
                    $.each(result, function (index, item) {
                        if (name==item.dockerName){
                            $('#dockerId option[value=' + item.id + ']').attr('selected', true)
                        }
                    })
                }
                form.render('select');
            },
            error: function () {
                alert("异常！");
            }
        });
    }
    // 监听下拉框改变事件,修改输入框中的内容
    form.on("select(dockerId)",function(data){
        let value=data.value
        let selectObj=dockerData.filter(item=>{
            return item.id==value
        })
        $("#dockerName").val(selectObj[0].dockerName);
    })


    dbNameInput = function(e){
        const val = $("#dbName").val();
        getDb(val)
    }
    let dbData
    function getDb(dockerName) {
        const dbType = $("#dbType").val();
        $.ajax({
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/admDb/getByType?name="+dockerName+"&type="+dbType,//url
            success: function (result) {
                dbData = result.data
                $("#dbId").html("");
                var html = "";
                $("#dbId").html("<option value=\"\">请选择数据库</option>");
                if (result.data != null) {
                    $.each(result.data, function (index, item) {
                        html += "<option  value='" + item.id + "'>" + item.name + "</option>";
                    })
                }
                $("#dbId").append(html);
                form.render('select');
                if (dockerName != null && dockerName !=''){
                    $(".dbId").find(".layui-form-select").addClass("layui-form-selected")
                }
            },
            error: function () {
                alert("异常！");
            }
        });
    }
    // 监听下拉框改变事件,修改输入框中的内容
    form.on("select(dbId)",function(data){
        let value=data.value
        let selectObj=dbData.filter(item=>{
            return item.id==value
        })
        $("#dbName").val(selectObj[0].name);
    })

    // 监听数据库类型选择框的变化
    form.on('select(dbTypeChange)', function(data){
        getDb("");
    });
});


